{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <select name="plat" id="plat" lay-filter="plat">
            <option value="">渠道类型（全部）</option>
            <option value="cps">CPS</option>
            <option value="sdk">联运</option>
        </select>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:channel type='manage' /}
        </div>
    </div>
    <div class="layui-inline" style="width:210px;">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
        <button class="layui-btn my-btn my-btn-sm" type="button" id="add">添加</button>
        <button class="layui-btn my-btn my-btn-sm" type="button" id="relation">关联渠道</button>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-green layui-btn-sm" lay-event="edit">编辑</button>
    <button class="layui-btn layui-btn-normal layui-btn-sm" lay-event="limit">限制礼包数量</button>
    <button class="layui-btn layui-btn-danger layui-btn-sm" lay-event="del">删除</button>
</script>

<script type="text/html" id="switchTpl">
    <input type="checkbox" name="status" value="{{d.cps_id}}" lay-skin="switch" lay-text="正常|禁用" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
{/block}

{block name="css"}
<style>
</style>
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var jsonObj = {
                        "channel_id": $('#channel_id').val()
                        ,"plat": $('#plat').val()
                    };

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: jsonObj
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("ChannelAccount/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function () {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'channel_id', title: '渠道类型', width: 100}
                ,{field:'cps_id', title: '渠道ID', width: 100, sort:true}
                ,{field:'cps_name', title: '渠道名称', width: 200}
                ,{field:'cps_type', title: '渠道类型', width: 100}
                ,{field:'account', title: '登录账号', width: 150}
                ,{field:'pay_rate', title: '分成比例', width: 100}
                ,{field:'pid', title: '父级渠道', width: 200}
                ,{field:'add_time', title: '添加时间', width: 160}
                ,{field:'status', title: '状态', width: 100, sort:true, templet: '#switchTpl'}
                ,{title: '操作', align:'center', width: 300, toolbar:'#tableBar'}
            ]]
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //监听搜索栏
        $('#add').on('click', function(){
            layer.open({
                type: 2
                ,title: '添加'
                ,area: ['100%', '100%']
                ,content: '{:url("ChannelAccount/add")}'
            });
        })

        $('#relation').on('click', function(){
            layer.open({
                type: 2
                ,title: '关联渠道'
                ,area: ['800px', '500px']
                ,content: '{:url("ChannelAccount/relation")}'
            });
        })

        //修改状态
        form.on('switch(status)', function(obj){
            var status = obj.elem.checked ? 1 : 0
                ,load = layer.load();
            console.dir(obj);
            $.post('{:url("ChannelAccount/edit")}',{cps_id:obj.value, status:status}, function(json){
                layer.close(load);
                if(json.code==0){
                    layer.msg(json.msg, {icon: 1, time:1000})
                }else{
                    layer.msg(json.msg, {icon: 0, time:1000});
                }
            }, 'JSON').error(function(){
                layer.close(load);
                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
            });
        });


        //监听工具条
        table.on('tool(tableBox)', function(obj){
            var data = obj.data;

            if(obj.event === 'edit'){
                layer.open({
                    type: 2
                    ,title: '编辑'
                    ,resize: true
                    ,area: ['100%', '100%']
                    ,content: '{:url("ChannelAccount/edit")}?cps_id='+data.cps_id
                    ,cancel: function(){
                        $.post('{:url("ChannelAccount/index")}',{channel_id: data.cps_id}, function(json){
                            obj.update({
                                channel_id: json.data[0].channel_id
                                ,cps_id: json.data[0].cps_id
                                ,cps_name: json.data[0].cps_name
                                ,cps_type: json.data[0].cps_type
                                ,pid: json.data[0].pid
                            })
                        }, 'JSON').error(function(){
                            layer.close(load);
                            layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                        });
                    }
                });
            }else if(obj.event === 'limit'){
                layer.open({
                    type: 2
                    ,title: '限制礼包数量'
                    ,resize: true
                    ,area: ['800px', '600px']
                    ,content: '{:url("ChannelAccount/limit")}?cps_id='+data.cps_id+'&cps_name='+data.cps_name
                });
            }else if(obj.event === 'del'){
                layer.confirm('确定要删除吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("ChannelAccount/del")}',{cps_id: data.cps_id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            obj.del();
                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });
    });
</script>
{/block}